package com.wyd.jwpkglxt_ssm.controller;

import com.wyd.jwpkglxt_ssm.entity.Classroom;
import com.wyd.jwpkglxt_ssm.entity.Department;
import com.wyd.jwpkglxt_ssm.service.DepartmentService;
import com.wyd.jwpkglxt_ssm.util.ResultCode;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (Department)表控制层
 *
 * @author wyd
 * @since 2021-08-03 11:37:19
 */
@RestController
@RequestMapping("department")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Autowired
    private DepartmentService departmentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Department selectOne(Integer id) {
        return this.departmentService.queryById(id);
    }

    @GetMapping("department/page")
    public ResultUtil getAllDepartmentByPage(Integer pageNum, Integer pageSize,
                                            @RequestParam(required = false)String departmentName,
                                            @RequestParam(required = false)Integer departmentArea) {
        Department department = new Department().setDepartmentName(departmentName).setDepartmentArea(departmentArea);
        return ResultUtil.ok().data("pageInfo",this.departmentService.queryAllByPage(pageNum,pageSize,department));
    }

    @PostMapping("department")
    public ResultUtil addDepartment(@RequestBody Department department){
        if(!Objects.isNull(department)){
            Department insertDepartment = this.departmentService.insert(department);
            if(!Objects.isNull(insertDepartment)){
                return ResultUtil.ok().data("department",insertDepartment);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @PutMapping("department")
    public ResultUtil updateDepartment(@RequestBody Department department){
        if(!Objects.isNull(department)){
            Department updateDepartment = this.departmentService.update(department);
            if(!Objects.isNull(updateDepartment)){
                return ResultUtil.ok().data("department",updateDepartment);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @DeleteMapping("department/{departmentId}")
    public ResultUtil deleteDepartmentById(@PathVariable("departmentId")String ids){
        if(ids.contains("-")){
            String[] idsplit = ids.split("-");
            List<Integer> departmentIds = new ArrayList<>();
            for(String s : idsplit){
                departmentIds.add(Integer.parseInt(s));
            }
            boolean delDepartments = this.departmentService.deleteDepartmentBatch(departmentIds);
            if(delDepartments){
                return ResultUtil.ok();
            }
        }else if(!"".equals(ids)){
            Integer id = Integer.parseInt(ids);
            boolean delDepartment = this.departmentService.deleteById(id);
            if(delDepartment){
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @GetMapping("department")
    public ResultUtil getAllDepartment(){
        return ResultUtil.ok().data("departments",this.departmentService.queryAll(null));
    }
}